package com.example.opengldemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.example.opengldemo.ndk.MyNDKActivity;

import java.util.ArrayList;
import java.util.List;

public class MenuActivity extends AppCompatActivity {
    List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        initData();
        ListView viewById = findViewById(R.id.activity_menu);
        MyAdapter adapter = new MyAdapter();
        viewById.setAdapter(adapter);
    }

    private void initData() {
        list.add("NDK界面");
        list.add("new opengl");
        list.add("GLES界面");
    }



    private void check(View v) {
        String tag = v.getTag().toString();
        switch (tag){
            case "Bullet界面" :
                Toast.makeText(this, tag, Toast.LENGTH_SHORT).show();
                break ;
            case "NDK界面" :
                Toast.makeText(this, tag, Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this , MyNDKActivity.class));
                break ;
            case "GLES界面" :
                Toast.makeText(this, tag, Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this , MainActivity_EarthMoonStar.class));
                break ;
        }
    }


    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHold viewHold = null;
            if (convertView == null) {
                viewHold = new ViewHold();
                viewHold.mLayout = new LinearLayout(MenuActivity.this);
                viewHold.mLayout.setGravity(Gravity.CENTER_VERTICAL);
                viewHold.mLayout.setOrientation(LinearLayout.HORIZONTAL);
                String s = list.get(position);
                Button button = new Button(MenuActivity.this);
                button.setText(s);
                button.setTag(s);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        check(v) ;
                    }
                });

                viewHold.mLayout.addView(button, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

                convertView = viewHold.mLayout;
                convertView.setTag(viewHold);
            } else {
                viewHold = (ViewHold) convertView.getTag();
            }
            return convertView;
        }

        class ViewHold {
            LinearLayout mLayout ;
            Button mButton;
        }
    }
}
